package com.dream.service.user.web.processor.impl;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.dream.common.model.response.BaseListResponse;
import com.dream.common.model.response.BaseResponse;
import com.dream.common.model.response.ErrorCode;
import com.dream.common.model.result.AppResultBuilder;
import com.dream.service.user.api.StoreAddrService;
import com.dream.service.user.domain.model.StoreAddrModel;
import com.dream.service.user.web.model.Param;
import com.dream.service.user.web.processor.RequestProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
/**
 * 门店地址
 */
public class StoreAddrRequestProcessor implements RequestProcessor {

    StoreAddrService storeAddrService;
    private Logger logger = LoggerFactory.getLogger(StoreAddrService.class);

    public StoreAddrService getStoreAddrService() {
        return storeAddrService;
    }

    public void setStoreAddrService(StoreAddrService storeAddrService) {
        this.storeAddrService = storeAddrService;
    }

    @Override
    public JSONObject processor(HttpServletRequest req) {
        JSONObject result = new JSONObject();
        String cmd = req.getParameter(Param.CMD);
        try {
            if (cmd.equals(Param.StoreAddressCMD.STORE_ADDRESS_LIST)) {
                this.getStoreAddrList(req, result);
            } else if(Param.StoreAddressCMD.ADD.equals(cmd)){
                addAddrList(req,result);
            }else {
                AppResultBuilder.buildCmdErrorResult(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
            AppResultBuilder.buildResult(result,"",false, ErrorCode.UNKOWN);
        }
        return result;
    }

    //地址列表

    public void getStoreAddrList(HttpServletRequest req,JSONObject result){
      String city= req.getParameter("city");

        BaseListResponse baseListResponse = null;
        try {
            if(StringUtils.isEmpty(city)){
                AppResultBuilder.buildParamErrorResult(result,"city is empty","");
                return;
            }
            baseListResponse = storeAddrService.getStoreAddressList(city);
            if (baseListResponse.isSuccess()) {
                AppResultBuilder.buildSucResult(result,baseListResponse.getResult());
            }else{
                AppResultBuilder.buildErrorResult(result, baseListResponse.getError_code(), baseListResponse.getError_msg(), "");
            }
        } catch (Exception e) {
            e.printStackTrace();
            AppResultBuilder.buildProcessErrorResult(result, e.getMessage(), "Service Error");
        }

    }

    public void addAddrList(HttpServletRequest req,JSONObject result){
        String address = req.getParameter("address");
        String lat = req.getParameter("lat");
        String lng = req.getParameter("lng");
        String name = req.getParameter("name");
        String telephone = req.getParameter("telephone");
        if(StringUtils.isEmpty(address)){
            AppResultBuilder.buildParamErrorResult(result,"address is empty","");
            return;
        }
        if(StringUtils.isEmpty(name)){
            AppResultBuilder.buildParamErrorResult(result,"name is empty","");
            return;
        }
        if(StringUtils.isEmpty(telephone)){
            AppResultBuilder.buildParamErrorResult(result,"telephone is empty","");
            return;
        }
        if(StringUtils.isEmpty(lat) || StringUtils.isEmpty(lng)){
            AppResultBuilder.buildParamErrorResult(result,"lat or lng is empty","");
            return;
        }
            StoreAddrModel storeAddrModel = new StoreAddrModel();
        storeAddrModel.setAddress(address);
        storeAddrModel.setLat(Double.parseDouble(lat));
        storeAddrModel.setLng(Double.parseDouble(lng));
        storeAddrModel.setName(name);
        storeAddrModel.setTelephone(telephone);

        BaseResponse baseListResponse = null;
        try {
            baseListResponse = storeAddrService.addStoreAddress(storeAddrModel);
            if (baseListResponse.isSuccess()) {
                AppResultBuilder.buildSucResult(result,baseListResponse.getResult());
            }else{
                AppResultBuilder.buildErrorResult(result, baseListResponse.getError_code(), baseListResponse.getError_msg(), "");
            }
        } catch (Exception e) {
            e.printStackTrace();
            AppResultBuilder.buildProcessErrorResult(result, e.getMessage(), "Service Error");
        }

    }


}
